# !/usr/bin/python3
# _*_coding: utf-8_*_

import json


class Find(object):
    def __init__(self):
        self._rule_file = "constraint.json"
        self._rule_list = self._rule_list()

    def _rule_list(self):
        with open(self._rule_file, "r") as f:
            rules = json.load(f)
        rules = rules['data']
        return rules

    def get_type_relationship(self, box):
        res_type = None
        relationship = None
        for type_rule in self._rule_list:
            rule = type_rule['ruleResult']
            for key, value in type_rule['containKeys'].items():
                if "_" in value:
                    vs = value.split('_')
                    v = vs[0] in box and vs[1] in box[vs[0]]
                else:
                    v = value in box
                rule = rule.replace(key, str(v))
            res = eval(rule)
            if res:
                res_type = type_rule['type']
                if "relationship" in type_rule:
                    relationship = type_rule['relationship']
                break
        return res_type, relationship
